﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;
using System.Web.UI.DataVisualization.Charting;

public partial class common_after_login_home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((AfterLogin)this.Master).mainCategory = "HOME";
        
            // Insert code to create basic pie chart
            // See the post entitled "Pie Charts in ASP.NET" for full source code

            // Set pie labels to be outside the pie chart
            this.ChartPositive.Series[0]["PieLabelStyle"] = "Disabled";

            // Add a legend to the chart and dock it to the bottom-center
            this.ChartPositive.Legends.Add("Legend1");
            this.ChartPositive.Legends[0].Enabled = true;
            this.ChartPositive.Legends[0].Docking = Docking.Bottom;
            this.ChartPositive.Legends[0].Alignment = System.Drawing.StringAlignment.Center;

            // Show labels in the legend in the format "Name (### %)"
            this.ChartPositive.Series[0].LegendText = "#VALX (#PERCENT)";

            // By sorting the data points, they show up in proper ascending order in the legend
            this.ChartPositive.DataManipulator.Sort(PointSortOrder.Descending, ChartPositive.Series[0]);

           //ChartNegative
            this.ChartNegative.Series[0]["PieLabelStyle"] = "Disabled";

            // Add a legend to the chart and dock it to the bottom-center
            this.ChartNegative.Legends.Add("Legend1");
            this.ChartNegative.Legends[0].Enabled = true;
            this.ChartNegative.Legends[0].Docking = Docking.Bottom;
            this.ChartNegative.Legends[0].Alignment = System.Drawing.StringAlignment.Center;

            // Show labels in the legend in the format "Name (### %)"
            this.ChartNegative.Series[0].LegendText = "#VALX (#PERCENT)";

            // By sorting the data points, they show up in proper ascending order in the legend
            this.ChartNegative.DataManipulator.Sort(PointSortOrder.Descending, ChartNegative.Series[0]);

            if (!IsPostBack)
            {
            string custID = Session["userId"] as String;
            string savingAccountNo = Session["savingAccountNo"] as String;
            if (Util.IsBlank(custID)) return;

           
                AccountsManager manager = new AccountsManager();
                String accountNoH = manager.getInsuranceAccountNo(custID, "H");
                String accountNoA = manager.getInsuranceAccountNo(custID, "A");

                if (!Util.IsBlank(accountNoH))
                {
                    alertAmt(accountNoH,custID,savingAccountNo);

                }

                 if (!Util.IsBlank(accountNoA))
                {
                    alertAmt(accountNoA,custID,savingAccountNo);

                }

            Session["ALREADY_TERMINATE_ALERT"] = true;
            this.gv_alertList.DataSource = getData();
            this.gv_alertList.DataBind();
        }
    }


    protected void gv_alertList_SelectedIndexChanged(object sender, EventArgs e)
    {

        int index = this.gv_alertList.SelectedIndex;
        HiddenField hdAlertId = this.gv_alertList.SelectedRow.FindControl("hd_alertId") as HiddenField;
        int id = int.Parse(hdAlertId.Value);
        
        Alert alert = new Alert();
        alert.confirm(id);
        this.gv_alertList.DataSource = getData();
        this.gv_alertList.DataBind();
        
    }

    private System.Data.DataSet getData(){
        Alert alert = new Alert();
        return alert.getAllAlertInfo(Session["userId"] as String);
    }

    private void temninateOverDueInsured(String accountNo,String message,String savingAccount)
    {
        AccountsManager am = new AccountsManager();
        ArrayList overDueLists = am.checkOverDue(accountNo);

        TerminateBLL bll = new TerminateBLL();
        if (overDueLists != null && overDueLists.Count > 0)
        {
            foreach(int insuredId in overDueLists){
                bll.terminatePolicy(insuredId, message, savingAccount);

            }
           
        }

    }

    private void alertAmt(String account,String custID,String savingAccountNo){
         AccountsManager manager = new AccountsManager();

         Object alerted = Session["ALREADY_TERMINATE_ALERT"];
         Alert alert = new Alert();
         if (alerted == null || !(bool)alerted)
         {
             this.temninateOverDueInsured(account, "Premium Overdued", savingAccountNo);
         }
                decimal amount = manager.getNeedPayAmount(account);
                if (amount > 0)
                {
                    ArrayList insuredIds = manager.getInsuredIds(account);
                    int id = (int)insuredIds[0];

                    Hashtable info = manager.getInsuranceInfo(id);

                    DateTime lastPaymentDt = (DateTime)info["startDate"];

                    if (DateTime.Now.Day - lastPaymentDt.Day <= 7)
                    {
                       
                        alert.addAlertPayment(custID, "Please pay your INSURANCE PREMIUM for next month.", "../fund_transfer/saving_payment.aspx?amount=" + amount + "&accountNo=" + account);

                    }
                }
                else
                {
                    alert.deleteMsg(custID, "Please pay your INSURANCE PREMIUM for next month.");
                }
          

    }
}